<script setup lang="ts">
import { computed, onMounted, useTemplateRef } from 'vue';
import { useCultivation, useCultivationSkill } from '../../../../composables';
import { rarityTextColorClass } from '../../../../utils';
import { EVENTS, GameKernel } from '../../../../game';
import UpgradeModal from './upgrade.vue';
import PageContainer from '../../components/page.vue';

const { state: cultivationSkillState } = useCultivationSkill();
const { back } = useCultivation();

const upgradeModalRef = useTemplateRef('upgradeModalRef');

const mainCultivationSkill = computed(() => {
  const state =
    cultivationSkillState.value.cultivationSkillUpgradeProgressInfo.find(
      (item) => item.skill.isMain
    );
  if (!state) return undefined;
  return state;
});

const minorCultivationSkills = computed(() => {
  return cultivationSkillState.value.cultivationSkillUpgradeProgressInfo.filter(
    (item) => !item.skill.isMain
  );
});

function onUpgradeClick(skillId: string) {
  upgradeModalRef.value?.show(skillId);
}

onMounted(() => {
  GameKernel.inst.bus.emit(EVENTS.CULTIVATION_SKILL_UPDATE_UPGRADE_INFO);
});
</script>

<template>
  <page-container title="功法" :show-back-button="true" @back="back">
    <div class="flex flex-col overflow-auto">
      <div class="flex-none">
        <div class="p-2 font-bold">主修功法</div>
        <div class="px-2">
          <a-card v-if="mainCultivationSkill" size="small">
            <div
              :class="[
                mainCultivationSkill.skillConfig.rarity &&
                  rarityTextColorClass(mainCultivationSkill.skillConfig.rarity),
              ]"
            >
              {{ mainCultivationSkill.skillConfig.name }}
            </div>
            <div>
              <span>等级：</span>
              <span>
                <span>{{ mainCultivationSkill.skill.currentLevel || 1 }}</span>
                <span v-if="mainCultivationSkill.masteryLevelDisplayName">
                  ({{ mainCultivationSkill.masteryLevelDisplayName }})
                </span>
              </span>
            </div>
            <div class="flex">
              <div class="flex-none">效果：</div>
              <div class="flex-1 flex flex-col">
                <div>
                  获取修为: +{{
                    mainCultivationSkill.skillConfig.baseCultivationSpeed +
                    mainCultivationSkill.cumulativeSpeedBonus
                  }}({{
                    `${mainCultivationSkill.skillConfig.baseCultivationSpeed} + ${mainCultivationSkill.cumulativeSpeedBonus}`
                  }})
                </div>
              </div>
            </div>
            <div class="pt-2 flex justify-end flex-wrap gap-2">
              <a-button
                :disabled="mainCultivationSkill.isMaxLevel"
                @click="onUpgradeClick(mainCultivationSkill.skill.skillId)"
              >
                {{ mainCultivationSkill.isMaxLevel ? '已满级' : '升级' }}
              </a-button>
            </div>
          </a-card>
        </div>
      </div>
      <div class="flex-1 overflow-auto flex flex-col pb-2">
        <div class="flex-none p-2 font-bold">副修功法</div>
        <div class="flex-1 overflow-auto px-2">
          <div class="flex flex-col gap-2">
            <a-card
              v-for="item in minorCultivationSkills"
              :key="item.skillConfig.id"
              size="small"
            >
              <div>{{ item.skillConfig.name }}</div>
              <div>
                <span>等级：</span>
                <span>
                  <span>
                    {{ item.skill.currentLevel || 1 }}
                  </span>
                  <span v-if="item.masteryLevelDisplayName">
                    ({{ item.masteryLevelDisplayName }})
                  </span>
                </span>
              </div>
              <div class="flex" v-if="item.skill">
                <div class="flex-none">效果：</div>
                <div class="flex-1 flex flex-col">
                  <div>
                    获取修为: +{{
                      item.skillConfig.baseCultivationSpeed +
                      item.cumulativeSpeedBonus
                    }}({{
                      `${item.skillConfig.baseCultivationSpeed} + ${item.cumulativeSpeedBonus}`
                    }})
                  </div>
                </div>
              </div>
              <div class="pt-2 flex justify-end flex-wrap gap-2">
                <a-button
                  :disabled="item.isMaxLevel"
                  @click="onUpgradeClick(item.skill.skillId)"
                >
                  {{ item.isMaxLevel ? '已满级' : '升级' }}
                </a-button>
              </div>
            </a-card>
            <a-card v-if="minorCultivationSkills.length === 0">
              <div class="text-center">无</div>
            </a-card>
          </div>
        </div>
      </div>
      <upgrade-modal ref="upgradeModalRef"></upgrade-modal>
    </div>
  </page-container>
</template>
